package com.example.demo.common.com.example.demo;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

public class Demo01 {

    public static void main(String[] args) {
        //var t = window.btoa("https://b2bwork.baidu.com/?ucid=".concat(e.user_key));
        //Object(P["b"])("https://b2bwork.baidu.com/api/auth/login?url64=".concat(t), !0)

        Integer cuid = 28719616;

        try {
            String url64 = Base64.encode("https://b2bwork.baidu.com/?ucid=" + cuid);
            System.out.println(url64);
            //https://b2bwork.baidu.com/api/auth/login?url64=" + url64
            //https://b2bwork.baidu.com/?ucid=" + cuid
            //https://b2bwork.baidu.com/dashboard?ucid=" + cuid
            Connection.Response response = Jsoup.connect("https://b2bwork.baidu.com/api/auth/login?url64=" + url64)
                    .cookie("__cas__st__", "163fb0353f65819db9d0529388727c6573aeb2844f34398f5945ace6641be517858e0208a9d20f1bcc14985e")
                    .cookie("__cas__id__", "34953804")
                    .cookie("BDB2BID", "4DF95E29E07225DAC826EBD4D685CA47:FG=1")
                    .followRedirects(true)
                    .execute();
            System.out.println(response);

            Connection.Response response1 = Jsoup.connect("https://b2bwork.baidu.com/dashboard?ucid=" + cuid)
                    .cookie("__cas__st__", "163fb0353f65819db9d0529388727c6573aeb2844f34398f5945ace6641be517858e0208a9d20f1bcc14985e")
                    .cookie("__cas__id__", "34953804")
                    .cookie("BDB2BID", "4DF95E29E07225DAC826EBD4D685CA47:FG=1")
                    .followRedirects(true)
                    .execute();
        } catch (Exception e) {
            System.out.println("模拟访问挂了");
        }


        HttpResponse httpResponse = HttpUtil.createGet("https://b2bwork.baidu.com/api/auth/getUserInfo?froms=zz&ucid=" + cuid + "&timeStamp=" + System.currentTimeMillis())
                .cookie("BDB2BID=4DF95E29E07225DAC826EBD4D685CA47:FG=1; __cas__st__=163fb0353f65819db9d0529388727c6573aeb2844f34398f5945ace6641be517858e0208a9d20f1bcc14985e; __cas__id__=34953804;")
                .execute();
        System.out.println(httpResponse.body());
        JSONObject bodyJson = JSON.parseObject(httpResponse.body());
        Integer status = bodyJson.getInteger("status");
        if (status != 0) {
            System.out.println("接口挂了");
            return;
        }

        String token = bodyJson.getJSONObject("inf").getString("csrf_token");
        System.out.println(token);


        HttpResponse httpResponse1 = HttpUtil.createGet("https://b2bwork.baidu.com/api/daily/goods_sku360?token=" + token + "&froms=zz&ucid=" + cuid + "&timeStamp=" + System.currentTimeMillis() + "&clientType=all&range=30&p=2&s=10")
                .cookie("BDB2BID=4DF95E29E07225DAC826EBD4D685CA47:FG=1; __cas__st__=163fb0353f65819db9d0529388727c6573aeb2844f34398f5945ace6641be517858e0208a9d20f1bcc14985e; __cas__id__=34953804;")
                .execute();

        System.out.println(httpResponse1.body());
    }
}
